package com.irdstudio.efp.esb.api.rest.contractdownload;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.efp.edoc.service.facade.ContractFileService;
import com.irdstudio.efp.esb.api.bean.contractdownload.ReqContractDownloadBean;
import com.irdstudio.efp.esb.api.bean.contractdownload.RespContractDownloadBean;
import com.irdstudio.efp.esb.api.common.EsbFrontCommonController;
import com.irdstudio.efp.esb.api.common.EsbRespServiceBeanCreator;
import com.irdstudio.efp.esb.common.constant.RespRetCode;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;


/**
 * 需求编号：【2019D0519】
 * 问题编号：【】
 * 服务名称 ：30220003(贷款文件下载)<br>
 * 服务场景名称：合同下载 <br>
 * 服务描述：本服务适用于贷款合同的下载管理 <br>
 * 服务场景描述：合同下载<br>
 *
 * @author xiaojunhui
 * @version 0.1
 * @date 2019/5/23 14:46
 */
@RestController
@RequestMapping("/esb")
public class ContractDownLoadController extends EsbFrontCommonController<ReqContractDownloadBean, RespContractDownloadBean> {

    private static Logger logger = LoggerFactory.getLogger(ContractDownLoadController.class);

    @Autowired
    private ContractFileService contractFileService;

    /**
     * 合同下载
     * 1、校验必输参数
     * 2、校验下载策略（决定是走本地文件下载还是影像文件系统下载，根据up_load_file的数据去判断）
     * 2.1 本地下载：
     * 2.1.1 根据合同类型和申请流水号再结合文件命名规则去定位到合同文件
     * 合同类型：
     * 标准：
     * 1 授信合同
     * 2 一般借款合同
     * 3 贸易融资合同
     * 4 担保合同
     * 网贷：
     * 01 授信合同
     * 02 借款合同
     * 03 担保合同
     * 2.1.2 读取文件，转成base64
     * 2.1.3 封装数据返回
     * 2.2 影像系统下载：
     * 2.2.1 根据流水以及合同类型去查询up_load_file表上传成功的记录
     * 2.2.2 调用影像系统api进行批次文件查询以及下载
     *
     * @param esbReqServiceBean       esb请求对象
     * @param br1
     * @param reqContractDownloadBean 返回对象
     * @param br2
     * @param exception               异常信息
     * @return
     */
    @PostMapping("/SERVICE_30220003_04")
    public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult br1,
                                     @Validated @ModelAttribute("getReqEsbBodyBean") ReqContractDownloadBean reqContractDownloadBean,
                                     BindingResult br2, Exception exception) {
        if (Objects.nonNull(exception)) {
            logger.error("合同下载异常", exception);
            return sedCommonService.getEsbRespServiceBeanByException(esbReqServiceBean, exception, new RespContractDownloadBean(), RespRetCode.FAILE, EsbRetCodeStandard.JKYDCSJYBTG);
        }
        try {
            String s = contractFileService.downContractFile(reqContractDownloadBean.getPrdId(), reqContractDownloadBean.getContType(), reqContractDownloadBean.getApplySeq());
            //封装结果返回
            RespContractDownloadBean respContractDownloadBean = new RespContractDownloadBean();
            respContractDownloadBean.setPic(s);
            return sedCommonService.getSucessBean(esbReqServiceBean, respContractDownloadBean, RespRetCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            RespContractDownloadBean respContractDownloadBean = new RespContractDownloadBean();
            respContractDownloadBean.setRetCode(ESBConstant.FAILURE);
            respContractDownloadBean.setRetMsg(e.getMessage());
            return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
                    JSONObject.toJSONString(respContractDownloadBean, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteMapNullValue),
                    ESBConstant.FAILURE,
                    ESBConstant.FAILURE,
                    e.getMessage());
        }

    }

}
